<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 基本数据库操作
 * @version 1.0.0 
 * @author Jason.z
 * @copyright AnAn-Studio.Inc
 */
class Sale_model extends CI_Model {

    const USERS_TBL = "users";
    const STUDENTS_TBL = "students";
    const TRACKS_TBL = "tracks";

  	public function __construct() {
            parent::__construct();
    }

    /**
     * 取出所有线索
     */
    public function get_all_clues($where=NULL)
    {
    	$this->db->join(self::USERS_TBL,self::USERS_TBL.'.id = '.self::STUDENTS_TBL.'.consultant','LEFT');
    	$this->db->join(self::TRACKS_TBL,self::TRACKS_TBL.'.student_id = '.self::STUDENTS_TBL.'.student_id');
    	if(!empty($where)) {
    		$this->db->where($where);
    	}	
    	$query = $this->db->get(self::STUDENTS_TBL);
    	// echo $this->db->last_query();
    	return $query->result();
    }

    /**
     * 获取单个线索记录
     *
     * @param $track_id 跟踪ID
     * @return null
     */
    public function get_clue_by_id($clue_id)
    {
        if(is_numeric($clue_id)) {
            $this->db->where('student_id', $clue_id);
            $query = $this->db->get(self::STUDENTS_TBL);
            return $query->row();
        }
        return NULL;
    }

    /**
     * 获取单个跟踪记录
     *
     * @param $track_id 跟踪ID
     * @return null
     */
    public function get_track_by_id($track_id)
    {
        if(is_numeric($track_id)) {
            $this->db->join(self::STUDENTS_TBL, self::TRACKS_TBL.'.student_id='.self::STUDENTS_TBL.'.student_id');
            $this->db->where('track_id', $track_id);
            $query = $this->db->get(self::TRACKS_TBL);
            return $query->row();
        }
        return NULL;
    }

     /**
     * 插入某条线索
     *
     * @param $data 插入数据
     * @return null
     */
    public function insert_clue($data)
    {
        $this->db->insert(self::STUDENTS_TBL,$data);
        return $this->db->insert_id();
    }


    /**
     * 更新某条线索
     *
     * @param $clue_id 线索id
     * @param $data 更新数据
     * @return boolean
     */
    public function update_clue($clue_id,$data,$escape = 'TRUE')
    {
        $this->db->where('student_id',$clue_id);

        foreach ($data as $key => $value) {
            $this->db->set($key,$value,$escape);
        }
        $this->db->update(self::STUDENTS_TBL);
        return $this->db->affected_rows()>0;
    }

    /**
     * 插入某条跟踪
     *
     * @param $data 插入数据
     * @return null
     */
    public function insert_track($data)
    {
        $this->db->insert(self::TRACKS_TBL,$data);
        return $this->db->insert_id();
    }


    /**
     * 更新某条跟踪
     *
     * @param $track_id 跟踪id
     * @param $data 更新数据
     * @return boolean
     */
    public function update_track($track_id,$data,$escape = 'TRUE')
    {
        $this->db->where('track_id',track_id);

        foreach ($data as $key => $value) {
            $this->db->set($key,$value,$escape);
        }
        $this->db->update(self::TRACKS_TBL);
        return $this->db->affected_rows()>0;
    }

    /**
     * 获取学生的跟踪记录
     *
     * @param $clue_id 学生ID
     * @return mixed
     *
     */
    public function get_clue_tracks($clue_id)
    {
        $this->db->join(self::STUDENTS_TBL, self::TRACKS_TBL.'.student_id='.self::STUDENTS_TBL.'.student_id');
        $this->db->where( self::TRACKS_TBL.'.student_id',$clue_id);
        $query = $this->db->get(self::TRACKS_TBL);
        return $query->result();
    }

}